<?php

use App\Models\Invoice\InvoiceHistoryModel;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Jialeo\LaravelSchemaExtend\Schema;

class CreateInvoiceHistory extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('invoice_history', function (Blueprint $table) {
            $table->id();

            $table->unsignedTinyInteger("channel")->default(0)
                  ->comment(enumToMigrationComment(InvoiceHistoryModel::CHANNEL_TYPE_TEXT, "开票渠道"));

            $table->unsignedBigInteger("newInvoiceId")->comment("红冲新开蓝票id")->default(0);
            $table->string("orderId", 50)->default("")->comment("业务系统订单号");
            $table->unsignedBigInteger("shopId")->default(0)->comment("门店id");
            $table->unsignedTinyInteger("businessType")->default(0)
                  ->comment(enumToMigrationComment(InvoiceHistoryModel::BUSINESS_TYPE_TEXT, "开票业务来源"));
            $table->string("invoiceTotalAmount")->default(0)->comment("开票总金额");
            $table->string("invoiceTaxTotalAmount")->default(0)->comment("开票总税额");
            $table->unsignedTinyInteger("headerType")->default(0)
                  ->comment(enumToMigrationComment(InvoiceHistoryModel::HEADER_TYPE_TEXT, "发票抬头"));
            $table->unsignedTinyInteger("invoiceType")->default(0)
                  ->comment(enumToMigrationComment(InvoiceHistoryModel::INVOICE_TYPE_TEXT, "发票类型"));
            $table->string("mobile", 11)->comment("手机号码")->default("");
            $table->string("email", 120)->comment("邮箱")->default("");
            $table->string("invoiceTitle", 36)->comment("发票抬头")->default("");
            $table->string("companyTaxNumber", 36)->comment("公司税号")->default("");
            $table->string("companyRegisterAddress", 72)->comment("公司注册地址")->default("");
            $table->string("companyRegisterMobile", 36)->comment("公司注册电话")->default("");
            $table->string("companyDepositBank", 72)->comment("公司开户行")->default("");
            $table->string("companyDepositBankNum", 36)->comment("公司开户行账号")->default("");
            $table->json("businessDetails")->nullable(true)->comment("开票业务明细");

            $table->string("blueSerialNumber", 36)->comment("moreDo - 流水号")->default("");
            $table->string("blueInvoiceNumber", 72)->comment("moreDo - 发票号码")->default("");
            $table->unsignedBigInteger("blueInvoiceTime")->comment("moreDo - 开票日期")->default(0);
            $table->string("blueTaxValueTotal", 36)->comment("moreDo - 价税合计")->default("");
            $table->text("blueEwm")->comment("moreDo - 二维码明文")->nullable(true);
            $table->string("blueInvoiceFile", 120)->comment("moreDo - 蓝字发票文件地址")->default("");
            $table->string("blueInvoiceOssFile", 120)->comment("moreDo - 蓝字发票夸父oss文件地址")->default("");
            $table->json("blueInvoiceDetails")->comment("moreDo - 蓝字发票明细")->nullable(true);

            $table->string("redConfirmSerialNo", 36)->comment("moreDo - 红字确认单流水号")->default("");
            $table->string("redConfirmNo", 36)->comment("moreDo - 红字确认单编号")->default("");
            $table->string("redConfirmUuid", 36)->comment("moreDo - 红字确认单UUID")->default("");
            $table->string("redConfirmState", 12)->comment("moreDo - 红字确认单状态")->default("");
            $table->string("redInvoiceFile", 120)->comment("moreDo - 红字发票文件地址")->default("");
            $table->string("redInvoiceOssFile", 120)->comment("moreDo - 红字发票夸父oss文件地址")->default("");
            $table->json("redInvoiceDetails")->comment("moreDo - 红字发票明细")->nullable(true);

            $table->text("errorMsg")->comment("错误信息")->nullable(true);
            $table->string("remark")->comment("备注")->default("");
//            $table->unsignedTinyInteger("redChannel")
//                  ->comment(enumToMigrationComment(InvoiceHistoryModel::RED_CHANNEL_TEXT, "红冲渠道"))->default(0);

            $table->unsignedTinyInteger("status")->default(0)
                  ->comment(enumToMigrationComment(InvoiceHistoryModel::STATUS_TEXT, "发票状态"));
//            $table->unsignedTinyInteger("invalidStatus")->default(0)
//                  ->comment(enumToMigrationComment(InvoiceHistoryModel::INVALID_STATUS_TEXT, "发票作废状态"));

            $table->unsignedInteger('createdAt')->default(0)->comment('创建时间');
            $table->unsignedInteger('createdByUid')->default(0)->comment('创建人');
            $table->unsignedInteger('updatedAt')->default(0)->comment('修改时间');
            $table->unsignedInteger('updatedByUid')->default(0)->comment('修改人');
            $table->tinyInteger('isDeleted')->default(0)->comment('是否删除 0 否 1 是');

            $table->collation = 'utf8mb4_general_ci';
            $table->comment   = '开票记录';

            $table->index(['shopId'], 'idx_shopId');
            $table->index(['orderId'], 'idx_orderId');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('invoice_history');
    }
}
